package com.guldan.jaina.manager;

import java.util.List;

import com.guldan.jaina.dto.summarybatchrelation.SummaryBatchRelationDTO;
import com.guldan.jaina.utils.OrgBatchSummaryDomainConvertUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.guldan.core.manager.BaseManager;
import com.guldan.mapper.db.entity.SummaryBatchRelation;
import com.guldan.mapper.db.entity.SummaryBatchRelationExample;
import com.guldan.mapper.db.mapper.SummaryBatchRelationMapperExt;

/**
 * Created by Silas.
 * Date: 2016/12/3
 * Time: 17:36
 */
@Service
public class SummaryBatchRelationManager extends BaseManager {
    @Autowired
    private SummaryBatchRelationMapperExt summaryBatchRelationMapperExt;

    @Autowired
    private OrgBatchSummaryManager orgBatchSummaryManager;

    public List<SummaryBatchRelation> getBatchListBySummaryId(String summaryId) {
        SummaryBatchRelationExample example = new SummaryBatchRelationExample();
        SummaryBatchRelationExample.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(summaryId)) {
            criteria.andSummaryIdEqualTo(summaryId);
        }
        criteria.andIsDeleteEqualTo(false);
        List<SummaryBatchRelation> data = summaryBatchRelationMapperExt.selectByExample(example);
        return data;
    }

    public SummaryBatchRelationDTO getSummaryBatchRelationDTOByBatchId(Integer batchId) {
        SummaryBatchRelationExample example = new SummaryBatchRelationExample();
        SummaryBatchRelationExample.Criteria criteria = example.createCriteria();
        if (batchId != null) {
            criteria.andBatchIdEqualTo(batchId);
        }
        criteria.andIsDeleteEqualTo(false);
        List<SummaryBatchRelation> data = summaryBatchRelationMapperExt.selectByExample(example);
        if (data != null && data.size() > 0)
            return OrgBatchSummaryDomainConvertUtils.convert(data.get(0));
        return null;
    }

    public Boolean deleteByBatchId(Integer batchId) {
        SummaryBatchRelationExample example = new SummaryBatchRelationExample();
        SummaryBatchRelationExample.Criteria criteria = example.createCriteria();
        if (batchId != null) {
            criteria.andBatchIdEqualTo(batchId);
        }
        criteria.andIsDeleteEqualTo(false);

        List<SummaryBatchRelation> list = summaryBatchRelationMapperExt.selectByExample(example);
        if (list != null && list.size() > 0) {
            for (SummaryBatchRelation entity : list) {
                entity.setIsDelete(true);
                List<SummaryBatchRelation> summaryBatchRelations = getBatchListBySummaryId(entity.getSummaryId());
                if (summaryBatchRelations == null || summaryBatchRelations.size() == 0) {
                    orgBatchSummaryManager.delete(entity.getSummaryId());
                }
                summaryBatchRelationMapperExt.updateByPrimaryKey(entity);


            }
        }


        return true;
    }

}
